package javacompiler.syntax;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Data structure used by the compiler to store the tokens data
 * 
 * @author Armstrong Mardilson da Silva Goes
 *
 */
public class SymbolsTable {
	private static SymbolsTable instance;
	private Map<Long, JavaSymbolInfo> table;
	
	public SymbolsTable() {
		table = new HashMap<Long, JavaSymbolInfo>();
	}
	
	public static SymbolsTable getInstance() {
		if (instance == null) {
			instance = new SymbolsTable();
		}
		return instance;
	}
	
	public static void reset() {
		instance = null;
	}
	
	public boolean addIdentifier(Long id, JavaSymbolInfo info) {
		table.put(id, info);
		return true;
	}

	public void addSymbol(Long id, JavaSymbolInfo info) {
		table.put(id, info);
	}
	
	public boolean containsIdentifier(String idValue) {
		for (JavaSymbolInfo info : table.values()) {
			if (info.getType() == JavaSymbolInfo.LexType.Identifier) {
				if (info.getIdValue().equals(idValue)) {
					return true;
				}
			}	
		}
		return false;
	}

	public JavaSymbolInfo getSymbol(Long id) {
		return table.get(id);
	}
	
	@Override
	public String toString() {
		return table.toString();
	}
	
	/**
	 * Returns the type of the Symbol whose identifier 
	 * is id. It will search for symbols with same idValue 
	 * to get the type. It's used to get the identifier type 
	 * after its declaration.
	 * @param id
	 * @return
	 */
	public String searchTypeOfSymbol(Long id) {
		JavaSymbolInfo symbolsInfo = table.get(id);
		String idValue = symbolsInfo.getIdValue();
		
		for (JavaSymbolInfo info : table.values()) {
			if (idValue.equals(info.getIdValue()) && info.getVariableType() != null) {
				return info.getVariableType();
			}
		}
		
		return null;
	}
	
	public List<Long> searchSymbolsByIdValue(Long originalId) {
		JavaSymbolInfo originalInfo = table.get(originalId);
		List<Long> symbols = new LinkedList<Long>();
		
		for (Long id : table.keySet()) {
			JavaSymbolInfo info = table.get(id);
			if (info != null && info.getIdValue() != null && info.getIdValue().equals(originalInfo.getIdValue()) &&
						id != originalId) {
				symbols.add(id);
			}
		}
		
		return symbols;
	}
	
	/**
	 * Returns if the symbols whose identifiers are id1 and id2
	 * have the same list of parameters.
	 */
	public boolean sameParameters(Long id1, Long id2) {
		List<String> id1ParametersTypes = getParametersTypes(id1);
		List<String> id2ParametersTypes = getParametersTypes(id2);
		return id1ParametersTypes.equals(id2ParametersTypes);
	}

	private List<String> getParametersTypes(Long id) {
		List<String> types = new LinkedList<String>();
		for (JavaSymbolInfo info : table.get(id).getParametersInfo()) {
			types.add(info.getVariableType());
		}
		return types;
	}

	public boolean compatible(List<JavaSymbolInfo> parametersInfo1,
			List<JavaSymbolInfo> parametersInfo2) {
		for (int i = 0; i < parametersInfo1.size(); i++) {
			if (!parametersInfo1.get(i).getVariableType().equals(
						parametersInfo2.get(i).getVariableType())) {
				return false;
			}
		}
		return true;
	}
}
